package at.fhooe.mc.master.clc.server.domain.jpa;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.persistence.CollectionTable;
import javax.persistence.ElementCollection;
import javax.persistence.Embedded;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;

import org.datanucleus.api.jpa.annotations.Extension;

import at.fhooe.mc.master.clc.server.domain.AbstractTestable;
import at.fhooe.mc.master.clc.server.domain.IBigEntity;
import at.fhooe.mc.master.clc.server.domain.IEmbeddedEntity;

@Entity
public class BigEntityJpa extends AbstractTestable implements IBigEntity
{
	@Id
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	private Long id; // automatically populated by JPA

	// each 100 entries
	@Extension(vendorName = "datanucleus", key = "gae.unindexed", value = "true")
	private List<String> stringList1;

	@Extension(vendorName = "datanucleus", key = "gae.unindexed", value = "true")
	// JDO extension-annotation for unindexed fields
	private Map<String, String> stringMap1;

	@Embedded
	private EmbeddedJPAEntity embeddedEntity;
	// 10 entries

	// This is how collections are embedded by the JPY documentation
	// However this does not work here - might be because the google datastore
	// does not support JPA joins.
//	@ElementCollection(targetClass=EmbeddedJPAEntity.class)
//	@CollectionTable(
//			name="EmbeddedJPAEntity",
//			joinColumns=@JoinColumn(name="OWNER_ID")
//			)
//	private List<EmbeddedJPAEntity> embeddedEntityList;

	public BigEntityJpa()
	{
	}

	public BigEntityJpa(boolean generate)
	{
		if (generate)
		{
			stringMap1 = new HashMap<String, String>();
			stringList1 = new ArrayList<String>();

			for (int i = 0; i < 100; i++)
			{
				stringMap1.put("1_key" + i, "1_value" + i);
				stringList1.add("string_entry" + i);
			}

			embeddedEntity = new EmbeddedJPAEntity(true);
//			embeddedEntityList = new ArrayList<EmbeddedJPAEntity>();
//			for (int i = 0; i < 10; i++)
//			{
//				embeddedEntityList.add(new EmbeddedJPAEntity(true));
//			}
		}
	}

	@Override
	public Long getId()
	{
		return this.id;
	}

	@Override
	public Map<String, String> getStringMap1()
	{
		return this.stringMap1;
	}

	@Override
	public IEmbeddedEntity getEmbeddedEntity()
	{
		return this.embeddedEntity;
	}

	@Override
	public List<? extends IEmbeddedEntity> getEmbeddedEntityList()
	{
//		return this.embeddedEntityList;
		return null;
	}

	@Override
	public List<String> getStringList1()
	{
		return this.stringList1;
	}

}
